<template>
  <!--1.首先，弹窗页面中要有el-dialog组件即弹窗组件，我们把弹窗中的内容放在el-dialog组件中-->
  <!--2.设置:visible.sync属性，动态绑定一个布尔值，通过这个属性来控制弹窗是否弹出-->
  <el-dialog title="新增用户" :visible.sync="detailVisible" width="35%" :style="{borderRadius: '25px',padding: '10px'}">
    <div>
      <form>
        <div class="outline">
          <div>
            <label for="title">ID:</label>
            <input type="text" id="title" v-model="formMsg.id">
          </div>
          <div>
            <label for="title">姓名:</label>
            <input type="text" id="title" v-model="formMsg.name">
          </div>

          <div>
            <label for="title">学工号:</label>
            <input type="text" id="title" v-model="formMsg.number">
          </div>
          <div>
            <label for="title">电话:</label>
            <input type="text" id="title" v-model="formMsg.phone">
          </div>
          <div>
            <label for="title">邮箱:</label>
            <input type="text" id="title" v-model="formMsg.email">
          </div>
          <div>
            <label for="title">余额:</label>
            <input type="text" id="title" v-model="formMsg.balance">
          </div>
          <div>
            <label for="condition">身份:</label>
            <input type="radio" name="type" value="学生" id="public" v-model="formMsg.type">
            <label for="public">学生</label>
            <input type="radio" name="type" value="老师" id="private" v-model="formMsg.type">
            <label for="private">老师</label>
          </div>
          <div>
            <input type="button" value="取消" class="cancel" @click="detailVisible=false">
            <input type="button" value="保存" class="store" @click="onSubmit">
          </div>
        </div>
      </form>
    </div>
  </el-dialog>
</template>

<script>
import axios from "axios";

export default {
  name: "dialogComponent",
  data() {
    return {
      detailVisible: false,
      formMsg:{
        id:"",
        number:"",
        phone:"",
        type:"",
        email:"",
        balance:"",
        name:""
      }
    }
  },
  methods: {
    //3.定义一个init函数，通过设置detailVisible值为true来让弹窗弹出，这个函数会在父组件的方法中被调用
    init(data) {
      this.detailVisible = true;
      //data是父组件弹窗传递过来的值，我们可以打印看看
      console.log(data);
    },

    onSubmit(){
      axios({
        method:"get",
        url:"http://localhost:5000//admin/reader/add",
        params:{
          reader_id:this.formMsg.id,
          number:this.formMsg.number,
          email:this.formMsg.email,
          balance:this.formMsg.balance,
          phone:this.formMsg.phone,
          identity:this.formMsg.type,
          name:this.formMsg.name,
          admin_id:"27"
        }
      }).then((res)=> {
        if(res.data.data ==='用户添加成功'){
          this.detailVisible = false
          this.$router.go(0)
          this.$message({
            message: res.data.data,
            type:"success"
          })
        }else {
          this.$message({
            message: res.data.data,
            type: "info"
          })

        }
      })
    }
  }
}
</script>
<style scoped>
el-dialog { border-radius: 10px; }
.outline{
  padding: 2%;
  display: flex;
  flex-direction: column;
  width: 450px;
  height: 320px;
}
.outline>div{
  margin:1.5% 10% 1.5% 2%;
  width: 240px;
  height: 40px;

}
label{
  vertical-align: top;
  margin-right: 2%;
}
#title{
  border-radius: 15px;
  background-color: rgb(237,237,237);
  margin-left: 5px;
  border: none;
  padding: 5px;

}
div>input[type=text]{
  float: right;

  //margin-left: auto;
}
div>input[type=radio]{
  margin-left: 30px;
}
.cancel{
  display: inline-block;
  width: 100px;
  height: 40px;
  margin: 10px;
  position: relative;
  left: 40%;
  background-color: rgb(166,166,166);
  border-radius: 15px;
  border: none;
  color: white;
  cursor: pointer;
}
.cancel:hover{
  background-color: red;
}
.store{
  display: inline-block;
  width: 100px;
  height: 40px;
  margin: 10px;
  position: relative;
  left: 40%;
  background-color: rgb(125,153,191);
  border-radius: 15px;
  border: none;
  color: white;
  cursor: pointer;
}
.store:hover{
  background-color: rgb(85,153,191);
}
</style>